#!/usr/bin/env python 2.7
# -*- coding: utf-8 -*-
# @Time      :2020/11/27
# @Author    :Erinyu
# @Email     :yuailing@bbdservice.com
# @File      :IC_UpaidLoanBal
# @Software  :PyCharm

'''eof
name:未结清贷款余额
code:IC_UpaidLoanBal
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import json
import sys
import re
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

def NaNCheck(str):
    return False if str in null_type_list else True

def intCheck(str):
    try:
        int(str)
        return True
    except:
        return False

# 未结清贷款余额 =
# 非循环贷账户汇总信息段(PC02E)[0.1]·余额(PC02EJ02)[1.1]
# +循环额度下分账户汇总信息段(PC02F)[0.1]·余额(PC02FJ02)[1.1]
# +循环贷账户汇总信息段(PC02G)[0.1]·余额(PC02GJ02)[1.1]
# PCO[1.1] - PC02[0.1] - (PC02E)[0.1]/(PC02F)[0.1]/(PC02G)[0.1] - (PC02EJ02)[1.1]/(PC02FJ02)[1.1]/(PC02GJ02)[1.1]

def IC_UpaidLoanBal():
    data = IC_DSJ.get('data')
    factor = []
    # 信贷交易信息概要 PCO [1.1]
    PCO = getTable(data, 'PCO', 1, 0)
    if PCO == [{}]:
        return u'缺失值'
    elif PCO == 0:
        return 0
    else:
        # 信贷交易信息概要信息单元 PC02[0.1]
        PC02 = getTable(PCO[0], 'PC02', 0, 0)
        if PC02 == [{}]:
            return u'缺失值'
        elif PC02 == 0:
            return 0
        else:
            # 非循环贷账户汇总信息段 PC02E[0.1]
            PC02E = getTable(PC02[0], 'PC02E', 0, 1)
            if PC02E == [{}]:
                factor.append(None)
            elif PC02E == 0:
                factor.append(0)
            else:
                # 余额(PC02EJ02)[1.1]
                PC02EJ02 = PC02E[0].get("PC02EJ02")
                if intCheck(PC02EJ02):
                    factor.append(int(PC02EJ02))
                else:
                    factor.append(None)
            # 循环额度下分账户汇总信息段 PC02F[0.1]
            PC02F = getTable(PC02[0], 'PC02F', 0, 1)
            if PC02F == [{}]:
                factor.append(None)
            elif PC02F == 0:
                factor.append(0)
            else:
                # 余额(PC02FJ02)[1.1]
                PC02FJ02 = PC02F[0].get("PC02FJ02")
                if intCheck(PC02FJ02):
                    factor.append(int(PC02FJ02))
                else:
                    factor.append(None)
            # 非循环贷账户汇总信息段 PC02G[0.1]
            PC02G = getTable(PC02[0], 'PC02G', 0, 1)
            if PC02G == [{}]:
                factor.append(None)
            elif PC02G == 0:
                factor.append(0)
            else:
                # 余额(PC02GJ02)[1.1]
                PC02GJ02 = PC02G[0].get("PC02GJ02")
                if intCheck(PC02GJ02):
                    factor.append(int(PC02GJ02))
                else:
                    factor.append(None)

    factor = [x for x in factor if x is not None]
    return u'缺失值' if len(factor) == 0 else sum(factor)

if __name__ == '__main__':
    file_obj = open(r'IC_DSJ.json', 'r')
    content = file_obj.read()
    IC_DSJ = json.loads(content, strict=False)
    IC_DSJ = IC_DSJ.get('IC_DSJ')
    res = IC_UpaidLoanBal()
    print(res)

result = IC_UpaidLoanBal()